package com.intelligentclass.mydata;

import com.intelligentclass.entity.Teaching;
import com.intelligentclass.requestItem.HomeworkItem;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyJson {

    private int status;
    private String message;
    private Map<String, Object> data;

    public MyJson(){
        message = new String();
        data = new HashMap<String, Object>();
    }

    public MyJson(int status, String message, Map<String, Object> data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public Map<String, ?> getData() {
        return data;
    }
    public void setData(Map<String, Object> data) {
        this.data = data;
    }
    public void addData(String key, Object value){
        data.put(key, value);
    }

    public String toString(){
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        builder.append("\"status\":");
        builder.append(status);
        builder.append(",");
        builder.append("\"message\":");
        builder.append("\"" + message + "\"");
        if(!data.isEmpty()) {
            builder.append(",");
            builder.append("\"data\":");
            builder.append("{");
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                builder.append("\"" + entry.getKey() + "\":");
                if (entry.getValue() instanceof String) {
                    builder.append("\"" + entry.getValue() + "\",");
                } else if (entry.getValue() instanceof Integer) {
                    builder.append(entry.getValue() + ",");
                } else if (entry.getValue() instanceof List) {
                    List list = (List) entry.getValue();
                    builder.append("[");
                    for (Object l : list) {
                        if (l instanceof CourseInfo) {
                            CourseInfo t = (CourseInfo) l;
                            builder.append(t.toString() + ",");
                        }
                        if (l instanceof HomeworkItem) {
                            HomeworkItem h = (HomeworkItem) l;
                            builder.append(h.toString() + ",");
                        }
                    }
                    builder.deleteCharAt(builder.length() - 1);
                    builder.append("],");
                } else {
                    builder.append("\"" + entry.getValue().toString() + "\",");
                }
            }
            builder.deleteCharAt(builder.length() - 1);
            builder.append("}");
        }
        builder.append("}");
        return builder.toString();
    }
}
